from re import compile
import pygame
import time
from tkinter import Tk
from XunfeiAPI import get_res
import tkinter
# 对话气泡系统
bubble_text = []
bubble_timer = []
def text_split(text,duration=3):
    global bubble_timer
    pages=[]
    lines=[]
    size=9
    j=0
    s=''
    pattern = compile(r'[^\x00-\xff]')
    for i in text:
        if j>=size or i=='\n':
            j=0
            if len(lines)==17:
                pages.append(lines)
                lines=[]
            lines.append(s)
            s=''
        if pattern.search(i):
            j+=1
        else:
            j+=0.5
        s=s+i
    if s != '':
        lines.append(s)
        pages.append(lines)

    for i in range(len(pages)):
        bubble_timer.append(time.time()+duration*(i+1))
        bubble_text.append(pages[i])


def output_bubble(font,screen):
    if len(bubble_timer)>0:
        if time.time() < bubble_timer[0] and bubble_text[0] is not None:
            text=bubble_text[0]
            pygame.draw.rect(screen, (10,0,0), (0, 29, 213, 20 + len(text) * 20+2), border_radius=10)
            pygame.draw.rect(screen, (255, 255, 240), (1, 30, 211, 20+len(text)*20),border_radius=10)

            # 绘制气泡箭头
            pygame.draw.polygon(screen, (10,0,0), [(209, 56), (209, 34), (231, 45)])
            pygame.draw.polygon(screen, (255, 255, 240), [(210, 55),(210, 35),(230, 45)])
            i=0
            # 绘制文字
            for line in text:
                text_surf = font.render(line, True, (50, 0, 0))
                screen.blit(text_surf, (15,40+i*20))
                i+=1

        elif time.time()>=bubble_timer[0]:
            del bubble_text[0]
            del bubble_timer[0]




def get_response(input_text):
    response = get_res(input_text)
    return response

class InputWindow:
    def __init__(self):
        self.content = ''
        self.root = Tk()
        self.root.attributes('-topmost', True)
        self.root.geometry("400x240+500+300")
        self.text = tkinter.Text(self.root, width=50, height=15, undo=True, autoseparators=False)

    def submit(self):
        self.content = self.text.get('1.0', tkinter.END)
        while self.content[-1:]=='\n':
            self.content=self.content[:-1]
        self.root.destroy()

    def get_input(self):
        self.text.pack()
        self.text.insert("insert", "")
        button = tkinter.Button(self.root, text="提交", command=self.submit)
        button.pack()
        self.root.mainloop()
        return self.content

class Output:
    def __init__(self,text):
        self.content=text
        self.root=Tk()
        self.root.attributes('-topmost', True)
        self.root.geometry("400x240+500+300")
        self.text = tkinter.Text(self.root, width=50, height=16, undo=True, autoseparators=False)
        self.text.insert(1.0, text)
        self.text.configure(state='disabled')

    def show(self):
        self.text.pack()
        self.root.mainloop()


